// pages/user/memberRegister/before.js
import requestUtil from '../../../baseRequest.js'
import urls from '../../../requestUrl.js'
import commonUtils from '../../../utils/util'

var openid = wx.getStorageSync('openid')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    checked:false,
    editable:false,
    getTelPhone:false,
  },
  toTheEnd(){
    this.setData({
      editable:true,
    })
  },
  onChange(event){
    this.setData({
      checked:!this.data.checked
    })
  },
  getPhoneNumber( detail ) {
    //不同意授权
    if(detail.detail.errMsg == 'getPhoneNumber:fail user deny'){
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '拒绝授权您将无法注册',
      })
      wx.navigateTo({
        url: '/pages/user/memberRegister/before',
      })
    }else if (detail.detail.encryptedData) {
        let param = detail.detail
        param.openid = wx.getStorageSync('openid');
        requestUtil.postRequest(urls.user.getPhone, param, (response) => {
          if(!commonUtils.checkTelphoneNumber(response)){
            wx.showModal({
              title: '提示',
              showCancel: false,
              content: '获取手机号码失败，请稍后操作',
            })
            wx.switchTab({
              url: '/pages/user/personal/index',
            })
          }else{
            wx.setStorageSync('telPhone', response);
            this.toRegister();
          }
        })
    }
},
  toRegister(){
    let telPhone = wx.getStorageInfoSync('telPhone');
    if(telPhone){
      wx.navigateTo({
        url: '/pages/user/memberRegister/index',
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _distributeCode = options.scene
    if(_distributeCode){
      getApp().saveDistributeCode(_distributeCode);
    }
    wx.login({
      complete: (res) => {
        requestUtil.getRequest(urls.user.getCode + res.code, {}, (resp) => {
          wx.setStorageSync('openid', resp.openid)
          console.log('success', resp)
      })
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})